package com.ruoyi.web.controller.function;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.entity.WarningRule;
import com.ruoyi.system.service.WarningRuleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author Y·C
 * @version 1.0.0
 * @ClassName WarningRuleController.java
 * @Description TODO
 * @createTime 2023年12月01日 09:37:00
 */
@Api(tags = "预警规则管理接口")
@RestController
@RequestMapping("/warningRule")
public class WarningRuleController extends BaseController {

    @Autowired
    private WarningRuleService warningRuleService;

    @ApiOperation(value = "查询预警规则信息")
    @GetMapping("/getWarningRuleList")
    @Anonymous
    public TableDataInfo getWarningRuleList(WarningRule warningRule) {
        startPage();
        List<WarningRule> warningRuleList = warningRuleService.getWarningRuleList(warningRule);
        return getDataTable(warningRuleList);
    }

    @ApiOperation(value = "根据id查询预警规则信息")
    @GetMapping("/getWarningRuleById/{id}")
    @Anonymous
    public AjaxResult getWarningRuleById(@PathVariable("id") Long id) {
        WarningRule warningRule = warningRuleService.getWarningRuleById(id);
        return AjaxResult.success(warningRule);
    }

    @ApiOperation(value = "根据id删除多条预警规则信息")
    @PostMapping("/deleteWarningRuleByIds")
    @Anonymous
    public AjaxResult deleteWarningRuleByIds(@RequestBody List<Long> ids) {

        return AjaxResult.success(warningRuleService.removeByIds(ids));
    }

    @ApiOperation(value = "新增预警规则信息")
    @PostMapping("/addWarningRule")
    @Anonymous
    public AjaxResult addWarningRule(@RequestBody WarningRule warningRule) {
        return toAjax(warningRuleService.save(warningRule));
    }

    @ApiOperation(value = "修改预警规则信息")
    @PostMapping("/updateWarningRule")
    @Anonymous
    public AjaxResult updateWarningRule(@RequestBody WarningRule warningRule) {
        return toAjax(warningRuleService.updateById(warningRule));
    }

    /**
     * 导出预警规则信息
     * */
    @ApiOperation(value = "导出预警规则信息")
    @RequestMapping("/export")
    @Anonymous
    public void export(HttpServletResponse response, WarningRule warningRule) {
        List<WarningRule> warningRuleList = warningRuleService.getWarningRuleList(warningRule);
        ExcelUtil<WarningRule> util = new ExcelUtil<>(WarningRule.class);
        util.exportExcel(response,warningRuleList, "预警规则数据");
    }


}
